#include "structs.h"
OUTPUT DinhThoi_SJF(INPUT input){
	OUTPUT output;
	int size = input.soProcess; // lay so process
//	int *wt; // cai nay dung de gan vao output
	int chon=0, lanthuchien=0, lanthu=0; // chon: vi tri cua phan tu co t/g cpu thap nhat dc dung de dinh thoi
	// lanthuchien: so lan cpu xu ly trong toan bo qua trinh dinh thoi	//// lanthu: so phan tu cua mang ket qua dinh thoi
	int i,j,k,l;
	int end = 0; // kiem tra dk dung
	for (i=0 ; i <size ; i++)
	{ 
		output.wtime[i]=0; 
	}
	for ( ; end == 0 ; )
	{ 
		end = 1;
		for (i=0 ; i <size ; i++)
		{ 
			if (input.pro[i].thoiGianXuLy !=0) end = 0 ; // kiem tra xem tat ca cac process da dc xu ly het chua
		} 
		if (end == 1 ) break; // neu xu ly het thi thoat.
		if (input.pro[chon].thoiGianXuLy == 0 || input.pro[chon].thoiDiemVaoHT > lanthuchien) 
		{ // kiem tra phan tu dc chon neu no ra ngoai hang doi hoac chua vao hang doi thi tim 1 thang thay the

			for (j=0 ; j<size ; j++) 
			{ 
				if (input.pro[j].thoiGianXuLy != 0)
					if(input.pro[j].thoiDiemVaoRQ+input.pro[j].thoiDiemVaoHT <= lanthuchien)
					{ 
						chon = j; break;  //thang nay du dk nen dung thay the
					} 
			} // lap het for neu ko tim dc thang nao thi qua while tang 1 thoi gian lanthuchien de kiem tra tiep
			while(input.pro[chon].thoiGianXuLy == 0 || (input.pro[chon].thoiDiemVaoHT + input.pro[chon].thoiDiemVaoRQ) > lanthuchien)
			{
				lanthuchien ++;
				for (l=0 ; l <size ; l++) 
				{
					if (input.pro[l].thoiGianXuLy !=0) 
						if (input.pro[l].thoiDiemVaoHT <= lanthuchien) // khi them 1 tg thuc hien thi thang nao da vao system thi no dang cho de thuc hien
							output.wtime[l]++;  // tang t/g cho cho no
				}
				for (j=0 ; j<size ; j++) 
				{ 
					if (input.pro[j].thoiGianXuLy != 0 && input.pro[j].thoiDiemVaoRQ+input.pro[j].thoiDiemVaoHT <= lanthuchien) 
					{ 
						chon = j; break; //tim thang thay the tiep lan nay lanthuchien da tang len
					} 
				}
			}
		}
		for (k=0 ; k<size ; k++)
		{ 
			if (input.pro[k].thoiDiemVaoRQ+input.pro[k].thoiDiemVaoHT <= lanthuchien) 
					if (input.pro[k].thoiGianXuLy != 0) 
						if (input.pro[k].thoiGianXuLy <= input.pro[chon].thoiGianXuLy) //kiem tra co thang nao co thoi gian cpu it hon thi chon no
							chon = k; 
		} 
		input.pro[chon].thoiGianXuLy--; //giam t/g thuc hien cua thang dc chon di 1
		output.sche[lanthu] = input.pro[chon].tenProcess; // dua no vao mang dinh thoi
		lanthu++;lanthuchien ++; //tang so pt cua mang va thoi gian thuc hien cua cpu len(lanthuchien) len
		for( l=0 ; l <size ; l++) 
		{
			if (l != chon) 
				if (input.pro[l].thoiGianXuLy !=0) 
					if (input.pro[l].thoiDiemVaoHT <= lanthuchien) // ngoai thang dc chon ra tat ca cac thang khac da vao deu cho
						output.wtime[l]++; // tang thoi gian cho cua may thang do len
		} 
	}
	output.avgtime=0.0;
	for (i=0 ; i <size ; i++ ) 
	{ 
		output.avgtime += (float)output.wtime[i]; //tong tinh thoi gian cho trung binh
	} 
	output.avgtime = output.avgtime / (float) size ;// tg cho t/binh
	output.ns=lanthu; //luu so pt cua mang dinh thoi
	output.nw=size; // luu so process

	return output;
}
